<script setup lang="ts">
import { computed } from 'vue'
import { PromoteInput } from '@/widgets'
import { getEntityIdList } from './core'

type Props = {
  type: string
  modelValue: string | null
  placeholder?: string
  promoteSize?: number
}

const props = withDefaults(defineProps<Props>(), {
  placeholder: '',
  promoteSize: 8
})

const emits = defineEmits<{
  (e: 'update:modelValue', v: string): void
}>()

const value = computed({
  get() {
    return props.modelValue || ''
  },
  set(v: string) {
    emits('update:modelValue', v)
  }
})

const idLst = getEntityIdList(props.type) || []

function promote(s: string): string[] {
  const size = props.promoteSize || 8
  let s1 = s.toLowerCase()
  const ret = []
  for (const e of idLst) {
    if (e.toLowerCase().indexOf(s1) >= 0) {
      ret.push(e)
      if (ret.length >= size) {
        break
      }
    }
  }
  return ret
}
</script>
<template>
  <PromoteInput v-model="value" :placeholder="placeholder" :promote="promote" />
</template>
